package com.commerce.constellation.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.commerce.constellation.R;

public class HeartLayout extends RelativeLayout {
    private AbstractPathAnimator mAnimator;
    public HeartLayout(Context context) {
        super(context);
        init(null, 0);
    }
    public HeartLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }
    public HeartLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }
    private void init(AttributeSet attrs, int defStyleAttr) {
        //检索此上下文主题中的样式属性信息
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.HeartLayout, defStyleAttr, 0);
        //路径动画
        mAnimator = new PathAnimator(AbstractPathAnimator.Config.fromTypeArray(a));
        //回收TypedArray，供以后的调用方重复使用。调用此函数后，您不能再触摸类型化数组
        a.recycle();
    }
    
    public void clearTheAnimation() {
        for (int i = 0; i < getChildCount()/*此视图的子视图个数*/; i++) {
            //取消此视图的任何动画
            getChildAt(i).clearAnimation();
        }
        //视图组中移除所有子视图
        removeAllViews();
    }
    public void addHeart(int color) {
        //一个心动画诞生于此
        HeartView heartView = new HeartView(getContext());
        heartView.setColor(color);
        mAnimator.start(heartView, this);
    }
}
